package com.linwen.excise.leetcode;

/**
 * @ClassName _1768交替合并字符串
 * @Description TODO
 * @Author zero
 * @DATE 2024/9/28 2:35 PM
 * @Version 1.0
 */
public class _1768交替合并字符串 {
    public static void main(String[] args) {
        _1768交替合并字符串 inst = new _1768交替合并字符串();
        String word1 = "abc", word2 = "pqr";
        System.out.println(inst.mergeAlternately(word1,word2));
    }
    public String mergeAlternately(String word1, String word2) {
        int minLength = Math.min(word1.length(), word2.length());
        char[] res = new char[word1.length() + word2.length()];
        int index = 0;
        for (int i = 0; i < minLength; i++) {
            res[index++] = word1.charAt(i);
            res[index++] = word2.charAt(i);
        }
        if (word1.length() > minLength) {
            for (int i = minLength; i < word1.length(); i++) {
                res[index++] = word1.charAt(i);
            }
        }
        if (word2.length() > minLength) {
            for (int i = minLength; i < word2.length(); i++) {
                res[index++] = word2.charAt(i);
            }
        }
        return new String(res);
    }
}
